<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/24
 * Time: 16:03
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\UserInvest;

class MeService {
	
	//投资记录数量
	public static function investInit($userID)
	{
		//投资成功的数目
		$data['yi']=UserInvest::find()->where(['userID'=>$userID,'status'=>2])->count();
		//投资中的数目
		$data['wei']=UserInvest::find()->where(['userID'=>$userID,'status'=>1])->count();
		return $data;
	}

    public static function simple($userID, $page = 1, $status = false, $limit = 10){
        $condition['userID'] = $userID;
        $condition['status'] = 1;
        if($status)
            $condition['status'] = 2;
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 10;
        $start = (intval($page) - 1) * $limit;
        $result = UserInvest::find()->where($condition)->offset($start)->limit($limit)->orderBy('id desc')->all();
        $list = array();
        if($result){
            $now = date('Ymd');
            $time = strtotime($now);
            foreach($result as $k=>$v){
                $vo = $v->attributes;
                //投资期限
                $_list['days']=ceil((strtotime($vo['endTime'])-strtotime($vo['startTime']))/86400);
                $_list['startTime'] = date('Y-m-d', strtotime($vo['startTime']));
                $_list['endTime'] = date('Y-m-d', strtotime($vo['endTime']));
                $_list['startTimeWeb'] = date('Y-m-d h:i:s', strtotime($vo['startTime']));
                $_list['endTimeWeb'] = date('Y-m-d h:i:s', strtotime($vo['endTime']));
                !$status && $_list['time'] = (strtotime($v['endTime']) - $time)/24/3600;
                //投资利率
                $_list['rate']=$vo['rate'];
                $_list['money'] = CommonFunction::formatMoney($vo['money']);
                $_list['accrual'] = CommonFunction::formatMoney($vo['accrual']);
                $_list['title'] = $vo['title'];
                $list[] = $_list;
            }
        }
        return $list;
    }
    public static function investList($userID, $page = 1, $status = false, $limit = 10){
        $condition['userID'] = $userID;
        $condition['status'] = 1;
        if($status)
            $condition['status'] = 2;
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 10;
        $start = (intval($page) - 1) * $limit;
        $result = UserInvest::find()->where($condition)->offset($start)->limit($limit)->orderBy('id desc')->all();
        $list = array();
        if($result){
            foreach($result as $k=>$v){
                $vo = $v->attributes;
				//投资期限
				$_list['days']=ceil((strtotime($vo['endTime'])-strtotime($vo['startTime']))/86400);
                $_list['startTime'] = date('Y-m-d', strtotime($vo['startTime']));
                $_list['endTime'] = date('Y-m-d', strtotime($vo['endTime']));
				$_list['startTimeWeb'] = date('Y-m-d h:i:s', strtotime($vo['startTime']));
                $_list['endTimeWeb'] = date('Y-m-d h:i:s', strtotime($vo['endTime']));
				//投资利率
				$_list['rate']=$vo['rate'];
                $_list['money'] = CommonFunction::formatMoney($vo['money']);
                $_list['accrual'] = CommonFunction::formatMoney($vo['accrual']);
                $_list['title'] = $vo['title'];
                $list[] = $_list;
            }
        }
        return ['list'=>$list];
    }
}